import sys
import typer

sys.path.append("../")  # noqa

from api.dataapi.task_ranking import task_top_by_month, task_top_ranking_v2

from config import col_activity, col_alumni, col_okr

app = typer.Typer()


def yield_month(year, s, e):
    for month in range(s, e + 1):
        yield {"year": year, "month": month}


@app.command()
def dept_ranking(year: int = typer.Option(..., help="年更新"),
                 s: int = typer.Option(1, help="开始月份"),
                 e: int = typer.Option(12, help="结束月份")):
    """
    >::计算排行表得分
    """
    okr = col_okr.find().distinct("dept_id")
    with typer.progressbar(okr) as progress:
        for ok in progress:
            dept_id = str(ok)
            for range_param in yield_month(year, s, e):
                task_top_by_month(inst_id=dept_id, **range_param)


@app.command()
def dept_ranking_v2(year: int = typer.Option(..., help="年更新")):
    """折算后新榜单（同1个单位同1年份内全部活动联系同一个科技者最多算2次）"""
    okr = col_okr.find().distinct("dept_id")
    with typer.progressbar(okr) as progress:
        for ok in progress:
            dept_id = str(ok)
            task_top_ranking_v2(dept_id, year=year)


if __name__ == '__main__':
    app()
